//
//  YWFunctionSettingTableViewController.m
//  maintain
//
//  Created by 烟雨云渊 on 17/1/5.
//  Copyright © 2017年 ewin. All rights reserved.
//

#import "YWFunctionSettingTableViewController.h"

#import "UIColor+CustomColors.h"
#import "YWFunctionManager.h"
#import "YWFunctionModel.h"
//#import "UITableView+FDTemplateLayoutCell.h"

#import "YWFunctionTableViewCell.h"

@interface YWFunctionSettingTableViewController ()<YWFunctionTableViewCellDelegate>
@property (nonatomic, strong) NSMutableArray * dataSource;
@end

@implementation YWFunctionSettingTableViewController
- (instancetype)init{
    return [super initWithStyle:UITableViewStyleGrouped];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"功能设置";
    [self setupTableView];
}

- (void)setupTableView{
    self.tableView.backgroundColor = [UIColor customBackgroudColor];
    self.tableView.rowHeight = 48.0f;
    self.tableView.separatorColor = [UIColor customLineColor];
    [self.tableView registerClass:[YWFunctionTableViewCell class] forCellReuseIdentifier:YWFunctionTableViewCellIdentifier];
}

- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
        NSArray * functionArray = [[YWFunctionManager shareManager] getFunctionMenuWithOffOrOn:YWFunctionStatusOn isSetting:YES];//OffOrOn参数在这里传没什么用
        [_dataSource addObjectsFromArray:functionArray];
    }
    return _dataSource;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YWFunctionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:YWFunctionTableViewCellIdentifier];
    cell.delegate = self;
    cell.function = self.dataSource[indexPath.row];
    return cell;
}

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    return 48;
//}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}

#pragma mark - YWFunctionTableViewCellDelegate
- (void)clickSwitchEvent:(UITableViewCell*)cell switchButton:(UISwitch *)switchBtn offOrOn:(BOOL)offOrOn{
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    YWFunctionModel * function = self.dataSource[indexPath.row];
    NSString * message;
    if (function.functionId == YWFunctionIdKeepWatch || function.functionId == YWFunctionIdInspection || function.functionId == YWFunctionIdDetection || function.functionId == YWFunctionIdUpkeep || function.functionId ==YWFunctionIdRepair) {
        message = [NSString stringWithFormat:@"关闭%@功能\n您将不再收到新的%@任务\n并且无法使用%@功能",function.functionName,function.functionName,function.functionName];
    }else{
        message = [NSString stringWithFormat:@"关闭%@功能\n您将无法使用%@功能",function.functionName,function.functionName];
    }
    
    if (offOrOn == NO) {
        UIAlertController* alertController= [UIAlertController alertControllerWithTitle:@"警告" message:message preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alertController animated:YES completion:nil];
        [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            [switchBtn setOn:YES];
            [self updaFunctionOffOrOn:cell offOrOn:YES];
        }]];
        [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [switchBtn setOn:NO];
            [self updaFunctionOffOrOn:cell offOrOn:NO];
        }]];
    }else{
        [self updaFunctionOffOrOn:cell offOrOn:offOrOn];
    }
    
}

- (void)updaFunctionOffOrOn:(UITableViewCell*)cell offOrOn:(BOOL)offOrOn{
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    YWFunctionModel * function = self.dataSource[indexPath.row];
    function.offOrOn = offOrOn;
    [[YWFunctionManager shareManager] updateFunctionStatus:offOrOn withFunctionId:function.functionId];
    [[NSNotificationCenter defaultCenter] postNotificationName:KPostFunctionChangeNotification object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
